package com.mvc.web.user.controller.validator;

import org.springframework.stereotype.Repository;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.mvc.web.common.utils.MD5Util;
import com.mvc.web.common.utils.ToolsUtils;
import com.mvc.web.user.vo.LoginForm;

@Repository
public class LoginValidator implements Validator {
	@Override
	public boolean supports(Class<?> clazz) {
		return LoginValidator.class.equals(clazz);
	}
	@Override
	public void validate(Object obj, Errors es) {
		LoginForm vo = (LoginForm) obj;
		if(vo.getType()==null||(vo.getType()!=1&&vo.getType()!=2)){
			es.reject("1002", "请选择登录方式");return;
		}
		if(ToolsUtils.isEmpty(vo.getPhone())||ToolsUtils.ValidatePhone(vo.getPhone())==false){
			es.reject("1002", "请输入正确的手机号码");return;
		}
		if(vo.getType()==1&&(vo.getVerifyCode()==null||(vo.getVerifyCode()+"").length()!=6)){
			es.reject("1002", "请输入正确的验证码");return;
		}
		if(vo.getType()==2&&ToolsUtils.isEmpty(vo.getPassword())){
			es.reject("1002", "请输入登录密码");return;
		}else if(vo.getType()==2){
			vo.setPassword(MD5Util.string2MD5(vo.getPassword()));
		}
	}
}
